Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyproject.toml writer #150

Merged
merged 6 commits into from
Nov 28, 2023
Merged

pyproject.toml writer #150

merged 6 commits into from
Nov 28, 2023

Conversation

clavedeluna
Copy link
Contributor

@clavedeluna clavedeluna commented Nov 28, 2023

Overview

Add a pyproject.toml dependency writer

Description

  • Following the example of the requirements.txt writer, add one for pyproject.toml, IFF it has a [project] dependencies section in the file
  • the one interesting difference for this writer is that since we're using tomlkit to parse and update the toml file, we didn't have a way to get the line numbers that changed. For that, I created a custom function that uses the output of difflib.unified_diff and parses out the @@ -x,y +a,b @@ and any + ... lines.

Copy link

codecov bot commented Nov 28, 2023

Codecov Report

Merging #150 (d0827ce) into main (5b4ea90) will decrease coverage by 0.04%.
The diff coverage is 95.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #150      +/-   ##
==========================================
- Coverage   96.44%   96.40%   -0.04%     
==========================================
  Files          72       73       +1     
  Lines        3207     3256      +49     
==========================================
+ Hits         3093     3139      +46     
- Misses        114      117       +3     
Files Coverage Δ
...er/dependency_management/base_dependency_writer.py 96.55% <100.00%> (+0.71%) ⬆️
...demodder/dependency_management/pyproject_writer.py 100.00% <100.00%> (ø)
...r/dependency_management/requirements_txt_writer.py 100.00% <100.00%> (ø)
...modder/dependency_management/dependency_manager.py 90.90% <66.66%> (-3.83%) ⬇️
src/codemodder/diff.py 92.85% <91.30%> (-7.15%) ⬇️

@clavedeluna clavedeluna marked this pull request as ready for review November 28, 2023 12:00
@clavedeluna clavedeluna added this pull request to the merge queue Nov 28, 2023
Merged via the queue into main with commit b77acd6 Nov 28, 2023
13 checks passed
@clavedeluna clavedeluna deleted the writers branch November 28, 2023 14:10
Copy link
Member

@drdavella drdavella left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clavedeluna overall this work looks really nice but I'm adding some comments here since I didn't have the opportunity to review before this was merged.

tomlkit.dump(pyproject, f)

changes = [
Change(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be factored out into the base class now since it includes some fiddly logic that will be pretty much the same for each writer.

from codemodder.dependency import DefusedXML, Security
from codemodder.project_analysis.file_parsers.package_store import PackageStore


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have a test case somewhere showing whether or not we preserve comments since this is going to be pretty important.

@@ -27,6 +28,10 @@ def write(
return RequirementsTxtWriter(
self.dependencies_store, self.parent_directory
).write(dependencies, dry_run)
case "pyproject.toml":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel pretty strongly that these kinds of values should be represented by an enum. The fact that it is being used as cases for match really emphasizes that to me.

[f"{dep.requirement}" for dep in dependencies]
)
except tomlkit.exceptions.NonExistentKey:
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we probably ought to have some kind of debug logging either here or at the caller.

@clavedeluna clavedeluna mentioned this pull request Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants